package com.stone.tool.pdf;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.stream.Collectors;

/**
 * pdfbox
 * <p>
 * 可以读取出来由模版替换变量的信息
 *
 * @author stone
 * @date 2025/7/18 22:27
 */
public class PdfUtil2 {

    public static void main(String[] args) throws IOException {
        System.out.println("   main execute =================");

        String result = "C:\\Users\\stone\\Desktop\\" + System.currentTimeMillis() + ".csv";
        String fileDir = "C:\\Users\\stone\\Desktop\\aaa";

        File aaa = new File(result);
        aaa.createNewFile();
        ArrayList<String> list = new ArrayList<>();
        list.add("文件名");
        list.add("身份证号");
        list.add("合同编号");
        String collect = list.stream().collect(Collectors.joining(StrUtil.COMMA));
        FileUtil.appendString(collect, result, CharsetUtil.UTF_8);
        // linux换行符
        FileUtil.appendString("\n", result, CharsetUtil.UTF_8);

        File dirs = FileUtil.file(fileDir);
        File[] files = dirs.listFiles();
        for (File string : files) {
            System.out.println(string.getName());
            if (string == null) {
                continue;
            }
            File[] numTmp = string.listFiles();
            if (numTmp == null) {
                continue;
            }
            for (File numStr : numTmp) {
                System.out.println(numStr.getName());
                if (numStr == null) {
                    continue;
                }
                File[] files1 = numStr.listFiles();
                if (files1 == null) {
                    continue;
                }
                for (File sssFile : files1) {
                    if (sssFile == null) {
                        continue;
                    }
                    File target = FileUtil.file(sssFile);
                    System.out.println(target.getName());
                    System.out.println(target.getAbsolutePath());
                    try {
                        PdfUtil2.read(target.getAbsolutePath(), result);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                }
            }
        }


        System.out.println("done ============================");
    }

    public static void read(String absolutePath, String filePath) throws IOException {

        File file = new File(absolutePath);
        String s = readPdfText(new FileInputStream(file));
        System.out.println("===========");
        s = s.substring(s.indexOf("第1页"), s.indexOf("第2页"));
//        String[] split = s.split("\r\n");
        String[] split = s.split("\n");
//        System.out.println(split[split.length-5]);
        System.out.println(file.getName());
        System.out.println(split[split.length - 4]);
        System.out.println(split[split.length - 3]);
//        System.out.println(split[split.length-2]);
//        System.out.println(split[split.length-1]);

        ArrayList<String> list = new ArrayList<>();
        list.add(file.getName());
        list.add(split[split.length - 4]);
        list.add(split[split.length - 3]);
        String collect = list.stream().collect(Collectors.joining(StrUtil.COMMA));
        FileUtil.appendString(collect, filePath, CharsetUtil.UTF_8);
        FileUtil.appendString("\n", filePath, CharsetUtil.UTF_8);
    }

    /**
     * 读取PDF全部文本内容
     */
    public static String readPdfText(InputStream pdfStream) throws IOException {
        try (PDDocument document = PDDocument.load(pdfStream)) {
            PDFTextStripper stripper = new PDFTextStripper();
            return stripper.getText(document);
        }
    }

    /**
     * 读取PDF特定页面的文本
     */
    public static String readPdfTextByPage(InputStream pdfStream, int pageNumber) throws IOException {
        try (PDDocument document = PDDocument.load(pdfStream)) {
            PDFTextStripper stripper = new PDFTextStripper();
            stripper.setStartPage(pageNumber);
            stripper.setEndPage(pageNumber);
            return stripper.getText(document);
        }
    }

    /**
     * 读取PDF中特定关键词后的内容
     */
    public String readTextAfterKeyword(InputStream pdfStream, String keyword) throws IOException {
        try (PDDocument document = PDDocument.load(pdfStream)) {
            PDFTextStripper stripper = new PDFTextStripper();
            String fullText = stripper.getText(document);

            int index = fullText.indexOf(keyword);
            if (index == -1) {
                return null;
            }

            return fullText.substring(index + keyword.length()).trim();
        }
    }


}
